 subroutine getpqrvv_ri2(icore, din, irrepdin, rivv,rivv2, ioppq, ispin)
! for get <ab||cd> by ri with special d  
! icore must be larger than abc*isd
 use mod_iop
 use mod_symm
 use mod_orbit
 use mod_ri 
 implicit none 
 integer, intent(in) ::  din, irrepdin,ioppq, ispin
 real*4, intent(in) :: rivv(*), rivv2(*)
 real*4, intent(out) :: icore(*)

 integer irrep, n, a, b, c, d
 integer numa, numb, numc, numd
 integer numab, numcd, ioff, nabc, ioffd,ioffd2(2000, 8), ioffout
 integer irrepa, irrepb, irrepc, irrepd, numnb, i0, i1
 integer irrepab, irrepcd, nsize, ioffabc, numna
 integer ioffab(8), ioffaa(8), nreadab, ioffvv(8) 
 integer, external :: irpdso, idsymsoc, isymoffso
 real*8, external :: nrm2so
 
 zorb(1:8) = 0 
 zorb(1) = 1
! <ab|cd> = (ac|bd) 
! write(6,*)'norm of rivv', nsize, nrm2so(nsize, rivv, 1), nrm2so(nsize, rivv2,1)
if(ispin==1) then 
       nsize = idsymsoc( 1, naux8, zorb, 1, vrta, vrta, 1)
       nabc = idsymsoc(irrepdin, vrta, vrta, 1, vrta, zorb, 1)
       irrepd = irrepdin
       i0 = 1
       i1 = i0 + nabc*isd
!       write(6,*)'test in getpqrvv_ri', ioffd, i1, naux8
       i0 = 1
       i1 = i0 + max(nabc*isd, idsymsoc(irrepdin, naux8, zorb, 1, vrta, zorb, 1))
       call getpqrso_incore(icore, din, irrepd, naux8, zorb, 1, vrta, vrta, 1, rivv, 1)
       call VmnpqVmnrs_to_Vpqrs(rivv, icore, 1, irrepd, naux8, zorb, 1, vrta, vrta, 1, &
                     vrta, zorb, 1, icore(i1), irrepd, 1.d0, 0.d0)
!       write(6,*)'test in getpqrvv_ri'
       call sstgenso(icore(i1), icore,  nsize, vrta, vrta, vrta, zorb, icore,&
                     irrepd, '1324')
!       write(6,*)'test in getpqrvv_ri'
!<AB||CD> = <AB|CD> - <BA|CD>
       if(ioppq==0) then 
          call isymtrso(icore, icore(i1), vrta, vrta, 1, 1, vrta, zorb, 1, 0, irrepd)
          call axpyso(nabc, -1.d0, icore(i1), 1, icore, 1)
          call icompso(icore, icore(i1), vrta, vrta, 1, 0, vrta, zorb, 1, 1,irrepd) 
          nabc = idsymsoc(irrepd, vrta, vrta, 0, vrta, zorb, 1) 
          call myicopyso(icore(i1), icore, nabc)
       elseif(ioppq==2) then 
          call isymtrso(icore, icore(i1), vrta, vrta, 1, 1, vrta, zorb, 1, 0, irrepd)
          call axpyso(nabc, 1.d0, icore(i1), 1, icore, 1)
          call icompso(icore, icore(i1), vrta, vrta, 1, 2, vrta, zorb, 1, 1,irrepd) 
          nabc = idsymsoc(irrepd, vrta, vrta, 2, vrta, zorb, 1) 
          call myicopyso(icore(i1), icore, nabc)
       endif
elseif(ispin==2) then 
       nsize = idsymsoc( 1, naux8, zorb, 1, vrtb, vrtb, 1)
       nabc = idsymsoc(irrepdin, vrtb, vrtb, 1, vrtb, zorb, 1)
       irrepd = irrepdin
       i0 = 1
       i1 = i0 + nabc*isd
!       write(6,*)'test in getpqrvv_ri', ioffd, i1, naux8
       i0 = 1
       i1 = i0 + max(nabc*isd, idsymsoc(irrepdin, naux8, zorb, 1, vrtb, zorb, 1))
       call getpqrso_incore(icore, din, irrepd, naux8, zorb, 1, vrtb, vrtb, 1, rivv, 1)
       call VmnpqVmnrs_to_Vpqrs(rivv, icore, 1, irrepd, naux8, zorb, 1, vrtb, vrtb, 1, &
                     vrtb, zorb, 1, icore(i1), irrepd, 1.d0, 0.d0)
!       write(6,*)'test in getpqrvv_ri'
       call sstgenso(icore(i1), icore,  nsize, vrtb, vrtb, vrtb, zorb, icore,&
                     irrepd, '1324')
!       write(6,*)'test in getpqrvv_ri'
!<AB||CD> = <AB|CD> - <BA|CD>
       if(ioppq==0) then 
          call isymtrso(icore, icore(i1), vrtb, vrtb, 1, 1, vrtb, zorb, 1, 0, irrepd)
          call axpyso(nabc, -1.d0, icore(i1), 1, icore, 1)
          call icompso(icore, icore(i1), vrtb, vrtb, 1, 0, vrtb, zorb, 1, 1,irrepd) 
          nabc = idsymsoc(irrepd, vrtb, vrtb, 0, vrtb, zorb, 1) 
          call myicopyso(icore(i1), icore, nabc)
       elseif(ioppq==2) then 
          call isymtrso(icore, icore(i1), vrtb, vrtb, 1, 1, vrtb, zorb, 1, 0, irrepd)
          call axpyso(nabc, 1.d0, icore(i1), 1, icore, 1)
          call icompso(icore, icore(i1), vrtb, vrtb, 1, 2, vrtb, zorb, 1, 1,irrepd) 
          nabc = idsymsoc(irrepd, vrtb, vrtb, 2, vrtb, zorb, 1) 
          call myicopyso(icore(i1), icore, nabc)
       endif
elseif(ispin==3) then 
        if(ioppq==0.or.ioppq==2) then 
            write(6,*)'error in ioppq in getri_vv'
        endif
        nsize = idsymsoc( 1, naux8, zorb, 1, vrtb, vrtb, 1)
       nabc = idsymsoc(irrepdin, vrta, vrta, 1, vrtb, zorb, 1)
       irrepd = irrepdin
       i0 = 1
       i1 = i0 + nabc*isd
!       write(6,*)'test in getpqrvv_ri', ioffd, i1, naux8
       i0 = 1
       i1 = i0 + max(nabc*isd, idsymsoc(irrepdin, naux8, zorb, 1, vrtb, zorb, 1))
       call getpqrso_incore(icore, din, irrepd, naux8, zorb, 1, vrtb, vrtb, 1, rivv2, 1)
       call VmnpqVmnrs_to_Vpqrs(rivv, icore, 1, irrepd, naux8, zorb, 1, vrta, vrta, 1, &
                     vrtb, zorb, 1, icore(i1), irrepd, 1.d0, 0.d0)
!       write(6,*)'test in getpqrvv_ri'
       call sstgenso(icore(i1), icore,  nsize, vrta, vrta, vrtb, zorb, icore,&
                     irrepd, '1324')
 endif
  return
  end
